var TableArticles = function () {
    return {
        init: function () {
          
            var oTable = $('#table_common').dataTable({
                "aLengthMenu": [
                    [-1,5, 15, 20],
                    ["全部",'5条', '15条', '20条'] 
                ],
                "iDisplayLength": -1,
				"bSort":false,
				"serverSide":true,
				"aaSorting":[[6,'desc']],
                "sPaginationType": "bootstrap",
				//"bStateSave": true, 
                "oLanguage": {
					"sSearch":"搜索",
                    "sLengthMenu": "每页显示 _MENU_ 记录",    
					"sEmptyTable":"没有数据",
					"sInfo": "当前数据为从第 _START_ 到第 _END_ 条数据；总共有 _TOTAL_ 条记录",   
					"sInfoEmpty": "没有数据",   
					"sInfoFiltered" : "",
					"sInfoPostFix" : "",
					"sLoadingRecords":"",
					"sProcessing": "正在加载数据...", 
					"sZeroRecords": "没有检索到数据", 
                    "oPaginate": {   
						"sFirst": "首页",   
						"sPrevious": "上一页",   
						"sNext": "下一页",   
						"sLast": "尾页"  
					}   
                },
				"sAjaxSource":ajaxUrl,
				"sAjaxDataProp": "data",
				
				"aoColumns": [
					{"sTitle":'编号',"mDataProp": 'articleId'},
					{"sTitle":'标题',"mDataProp": 'articleTitle'},
					{"sTitle":'分类',"mDataProp": 'catId'},
					{"sTitle":'编辑者',"mDataProp": 'managerId'},
					{"sTitle":'创建时间',"mDataProp": 'createTime'},
					{"sTitle":'是否显示',"mDataProp": 'isShow'},
					{"sTitle":'操作',"mDataProp": 'createTime'}
				],
                "aoColumnDefs": [{
                        'bSortable': false,
						'aTargets': ['_all']
                    },
					{"aTargets":[0],"sClass":'text-center',"sWidth":'5%'},
					/* {"aTargets":[1],"mRender":function(data,type,full){
                     return '<img src="'+Upload+'/'+data.replace(".","_s.")+'"/>';
                    },"sClass":'text-center',"sWidth":'5%'}, */
					{"aTargets":[1],"mRender":function(data,type,full){						
							return '<a class="detail" href="">'+data+'</a>';
                    }},
					
					{"aTargets":[2],"mRender":function(data,type,full){						
							return full['parentName'];
                    }},
					{"aTargets":[3],"mRender":function(data,type,full){						
							return full['loginName'];
                    }},
					{"aTargets":[5],"mRender":function(data,type,full){
						
							if(data==1){
								
								return '<a href="#" class="isShow btn btn-xs blue">显示</a>';
								
							}else{
								return '<a href="#" class="isShow btn btn-xs default">隐藏</a>';
							}
                    },"sClass":'text-center',"sWidth":'5%'},
					
					{"aTargets":[6],"mRender":function(data,type,full){						
							return '<a class="edit btn btn-xs default" href=""><i class="fa fa-edit"></i> 修改</a><a class="delete btn btn-xs red" href=""><i class="fa fa-trash-o"></i> 删除</a>';
                    },"sClass":'text-center',"sWidth":'5%'}
                ],
				"fnCreatedRow":function(nRow,aData,iDataIndex){
					$('td:eq(0)',nRow).html(iDataIndex+1);//为第一个单元格增加序号
				}	
				
            });

            jQuery('#table_common_wrapper .dataTables_filter input').addClass("form-control input-medium input-inline"); // modify table search input
            jQuery('#table_common_wrapper .dataTables_length select').addClass("form-control input-small"); // modify table per page dropdown
            jQuery('#table_common_wrapper .dataTables_length select').select2({
                showSearchInput : false 
            }); 
			
			var nEditing = null;			
			//详细
            $('#table_common a.detail').live('click',function (e) {
				e.preventDefault();
                var nRow = $(this).parents('tr')[0];
				var aData = oTable.fnGetData(nRow);
				$('.modal-header h4')[0].innerHTML = aData['articleTitle'];
				$('.modal-body p')[0].innerHTML = aData['articleContent'];
				$('#responsive').modal('show');//显示模态框 				
            });
			
			//文章状态
			$('#table_common a.isShow').live('click', function (e) {
                e.preventDefault();
                var nRow = $(this).parents('tr')[0];
				var aData = oTable.fnGetData(nRow);
				var isShow = aData['isShow']==1?0:1;
				$.post(ajaxUrl,{articleId:aData['articleId'],isShow:isShow,ajax:'isShow'},function(data){
					if(data){
						layer.msg('文章状态修改成功！',{icon: 1,time: 2000});
						oTable.fnUpdate(isShow, nRow,5, false);
						oTable.fnDraw();
					}else{
						layer.msg('文章状态未修改！',{icon: 2,time: 2000});
						return;
					}
			   });
            });
			
			//删除
            $('#table_common a.delete').live('click', function (e) {
                e.preventDefault();
				var nRow = $(this).parents('tr')[0];
				var aData = oTable.fnGetData(nRow);
				layer.confirm('确定删除该文章？',function(index){
					$.post(ajaxUrl,{articleId:aData['articleId'],ajax:'delArticles'},function(data){
						if(data){
							layer.msg('文章删除成功！',{icon: 1,time: 2000});
							oTable.fnDeleteRow(nRow);
						}else{
							layer.msg('文章删除失败！',{icon: 1,time: 2000});
							return;
						}
				   });					
				});
            });

            $('#table_common a.edit').live('click', function (e) {
                e.preventDefault();
                var nRow = $(this).parents('tr')[0];
				var aData = oTable.fnGetData(nRow);
				var url=editUrl+'?articleId='+aData['articleId'];
				location.href=url;
            });
			
			
        }

    };

}();